package com.yingpai.fitness.dvp_2_0.main.fragment.fitness;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.fragment.CommonFragment;
import com.yingpai.fitness.dvp_2_0.common.util.SharedPreferencesHelp;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.adapter.BikeBottomPagerAdapter;
import com.yingpai.fitness.dvp_2_0.main.adapter.BikeTopPagerAdapter;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.RunBean;
import com.yingpai.fitness.entity.wx.TokenBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 骑行fragment
 */
public class FitnessBikeFragment extends CommonFragment implements OnRefreshListener {

    private Unbinder unbinder;
    private TokenBean token;
    @BindView(R.id.run_or_ride_knowledge_rv_list)
    ViewPager run_or_ride_knowledge_rv_list;
    @BindView(R.id.run_or_ride_topic_rv_list)
    ViewPager run_or_ride_topic_rv_list;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.knowledgeTv)
    TextView knowledgeTv;
    @BindView(R.id.topicTv)
    TextView topicTv;
    @BindView(R.id.mid_ll)
    LinearLayout mid_ll;
    @BindView(R.id.title_bar)
    RelativeLayout title_bar;
    @BindView(R.id.empty)
    LinearLayout empty;


    public static FitnessBikeFragment getInstance() {
        return new FitnessBikeFragment();
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dvp_2_0_bike, container, false);
        unbinder = ButterKnife.bind(this, view);
        title_bar.setVisibility(View.GONE);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setRefreshHeader(new MaterialHeader(context).setColorSchemeResources(R.color.common_color));
        token = (TokenBean) SharedPreferencesHelp.getBean(Constant.TOKENBEAN);
        knowledgeTv.setText("骑行必备知识");
        topicTv.setText("骑行话题");
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    protected void initData() {
        HttpParams httpParams = new HttpParams();
        httpParams.put("customerId", token.getId());
        httpParams.put("tagId", Constant.NUM_3);
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.FITNESS_FRAGMENT_DATA, httpParams, Url.FITNESS_FRAGMENT_DATA,
                new Callback<RunBean>() {
                    @Override
                    public void onSuccess(RunBean bean) {
                        refreshLayout.finishRefresh();
                        if (bean.getResult().equals(Constant.REQUEST_SUCCESS)) {
                            empty.setVisibility(View.GONE);
                            mid_ll.setVisibility(View.GONE);
                            /**
                             * 骑行必备知识
                             */
                            List<RunBean.MapBean.UpBannersBean> upBanners = bean.getMap().getUpBanners();
                            BikeTopPagerAdapter adapter = new BikeTopPagerAdapter(context, upBanners);
                            run_or_ride_knowledge_rv_list.setAdapter(adapter);
                            /**
                             * 骑行话题
                             */
                            List<RunBean.MapBean.DownBannersBean> downBanners = bean.getMap().getDownBanners();
                            BikeBottomPagerAdapter bottomPagerAdapter = new BikeBottomPagerAdapter(context, downBanners);
                            run_or_ride_topic_rv_list.setAdapter(bottomPagerAdapter);
                        }else{
                            empty.setVisibility(View.VISIBLE);
                            showToast(bean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        empty.setVisibility(View.VISIBLE);
                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        initData();
    }

    @Override
    @OnClick({R.id.empty})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.empty:
                onRefresh(refreshLayout);
                break;
        }
    }
}